package org.southyin.controller.cms_article;

import com.fasterxml.jackson.databind.ObjectMapper;
import org.southyin.commons.CommonsResp;
import org.southyin.model.CmsArticle;
import org.southyin.model.CmsCustomer;
import org.southyin.service.CmsArticleService;
import org.southyin.service.impl.CmsArticleServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

@WebServlet(name = "CmsPublishArticleServlet", value = "/article/save")
public class CmsPublishArticleServlet extends HttpServlet {
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        PrintWriter writer = resp.getWriter();
        String title = req.getParameter("title");
        String hmContent = req.getParameter("hmContent");
        String cid = req.getParameter("cid");
        String status = req.getParameter("status");
        // 作者的 id,不应该靠前端传，因为前端的参数是可以伪造的
        CmsCustomer loginer = ((CmsCustomer) req.getSession().getAttribute("loginer"));
        Long uid = loginer.getId();
        // 封装数据
        CmsArticle article = new CmsArticle();
        article.setTitle(title);
        article.setCid(Long.valueOf(cid));
        article.setHmContent(hmContent);
        article.setMdContent(hmContent);
        article.setStatus(Integer.valueOf(status));
        article.setUid(uid);
        // 调service
        CmsArticleService service = new CmsArticleServiceImpl();
        CommonsResp commonsResp = service.saveArticleInfo(article);
        writer.println(new ObjectMapper().writeValueAsString(commonsResp));
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }
}
